const db = require('../db/index')

// 定义补零的函数
const padZero = (n) => {
    return n > 9 ? n : '0' + n
}

const dataFormat = (date) => {
    const dt = new Date(date);

    var y = dt.getFullYear();
    var m = padZero(dt.getMonth() + 1);
    var d = padZero(dt.getDate());

    var hh = padZero(dt.getHours());
    var mm = padZero(dt.getMinutes());
    var ss = padZero(dt.getSeconds());

    return y + "-" + m + "-" + d + " " + hh + ":" + mm + ":" + ss;
}

// 查询所有文章
exports.getAllArticle = (req, res) => {
    const sql = 'select * from articles where isdelete = 0'
    db.query(sql, (err, result) =>{
        if(err) return res.show(err)
        // if(result.length === 0) return res.show("查询不到数据！")
        let dates = []
        result.forEach(data => {
            dates.push({ ...data, pubdate: dataFormat(data.pubdate)});
        });

        res.render('content/html', { articleData: dates })

    })
}

// 根据专业类别查询所有文章
exports.getArticleByCate = (req, res) => {
    console.log(req.params.id)
    const sql = 'select * from articles where majorId = ? and isdelete = 0'
    db.query(sql, req.params.id, (err, result) => {
        if(err) return res.show(err)
        // if(result.length === 0) return res.show("查询不到数据！")
        // res.send({
        //     status: 0,
        //     message: "查询成功！",
        //     data: result
        // })
        let dates = [];
        result.forEach((data) => {
          dates.push({ ...data, pubdate: dataFormat(data.pubdate) });
        });

        res.render("content/html", { articleData: dates });
    })
}


// 根据文章 ID 查询文章信息
exports.getArticleById = (req, res) => {
    const sql = 'select * from articles where articleId = ?'
    db.query(sql, req.params.articleId, (err, result) => {
        if(err) return res.show(err)
        if(result.length === 0) return res.show("查询不到数据！")
        res.send({
            status: 0,
            message: "查询成功！",
            data: result
        })
    })
}

// 根据文章内容或者标题检索文章
exports.getArticleBySearch = (req, res) => {
    // console.log(req.params)
    const content = req.params.search
    const sql = "select * from articles where title like '%" + content + "%' or content like '%" + content + "%'"
    db.query(sql, (err, result) => {
        let dates = [];
        result.forEach((data) => {
          dates.push({ ...data, pubdate: dataFormat(data.pubdate) });
        });
        res.render("content/html", { articleData: dates });
    })
}


// 修改
exports.updateArticle = (req, res) => {
    const sql = 'update articles set ? where articleId = ?'
    db.query(sql, [req.body, req.body.articleId], (err, result) => {
        if(err) return res.show(err)
        if(result.affectedRows !== 1) return res.show("修改文章失败！")
        res.show("修改文章成功！", 0)
    })
}


// 新增
exports.addArticle = (req, res) => {
    // console.log(req.file)
    const article = req.body;
    // console.log(article)
    const sql = "insert into articles set ?";
    db.query(sql,
        {
            authorId: req.user.userId,
            majorId: article.majorId,
            title: article.title,
            content: article.content,
            image: article.image
        },
        (err, result) => {
        if(err) return res.show(err)
        if(result.affectedRows !== 1) return res.show("新增文章失败！")
        console.log("成功")
        res.show("新增文章成功！", 0)
    })
}

// 删除
exports.deleteArticle = (req, res) => {
    // const sql = 'delete from articles where articleId = ?'
    const sql = 'update articles set isdelete = 1 where articleId = ?'
    db.query(sql, req.body.articleId, (err, result) => {
        if(err) return res.show(err)
        if(result.affectedRows !== 1) return res.show("删除文章失败！")
        res.show("删除文章成功！", 0)
    })
}